// 这个文件用来封装接口 发送请求
import axios from 'axios'
import { promises } from 'fs'

axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'

// export const login = function (obj) {
//   return axios.post('login', obj)
// }
export const login = obj => axios.post('login', obj)
// 使用请求拦截 作用将所有的请求拦截下来 执行自己的逻辑
axios.interceptors.request.use(function (config) {
  let token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  return promises.reject(error)
})
export const getUserList = obj => axios.get('users', { params: obj })

// 添加用户
export const getAddList = obj => axios.post('users', obj)

// 删除用户
export const delList = id => axios.delete(`users/${id}`)

// 编辑用户
export const editList = obj => axios.put(`users/${obj.id}`, obj)

// 获取角色列表
export const getRoleList = () => axios.get('roles')

// 分配用户角色
export const getUsers = (id, rid) => axios.put(`users/${id}/role`, { rid: rid })

// 获取权限列表数据
export const getRights = type => axios.get(`rights/${type}`)

export const claseList = (roleId, rightId) => axios.delete(`roles/${roleId}/rights/${rightId}`)

// 左侧菜单部分数据
export const getMenus = () => axios.get('menus')

// 角色授权
export const grantRoleRight = (roleId, rids) => axios.post(`roles/${roleId}/rights`, { rids })

// 商品分类数据
export const categoriesList = (type, pagenum, pagesize) => axios.get('categories', { params: { type, pagenum, pagesize } })
